//
//  MailDetailViewController.m
//  Utils
//
//  Created by enghou on 2018/4/13.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

#import "MailDetailViewController.h"
#import "NSString+size.h"
#import "NetWorkerManager.h"
@interface MailDetailViewController ()

@property(nonatomic,strong)NSMutableArray *contents;

@end

@implementation MailDetailViewController

-(void)delAction{
    [self beginLoading];
    [[NetWorkerManager sharedInstance]delMailWithId:self.mailId successBlock:^(BaseModel *model) {
        [self endLoading];
        if (model.success) {
            //让前一个页面刷新
            //弹出
            if ([self.theDelete respondsToSelector:@selector(delAItemWithId:)]) {
                [self.theDelete delAItemWithId:self.mailId];
            }
            [self pop];
        }else{
            [self commonFailBlockWithModel:model];
        }
    } failBlock:^(NSInteger code) {
        [self endLoading];
        [self alertSome:kNetWorkFail];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    UIBarButtonItem *trash = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(delAction)];
    trash.tintColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = trash;
    self.contents = [NSMutableArray array];
    self.tableView.tableFooterView = [[UIView alloc]init];
    self.tableView.backgroundColor = [UIColor defaultBackgroundColor];
    [self loadDetail];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

-(void)loadDetail{
    [self beginLoading];
    [[NetWorkerManager sharedInstance]getMailWithId:self.mailId successBlock:^(BaseModel *model) {
        [self endLoading];
        if(model.success){
            NSDictionary *source = model.data;
            NSDictionary *data = [source objectForKey:@"data"];
            NSString *sender = [data objectForKey:@"sender"];
            if (sender) {
                [self.contents addObject:sender];
            }else{
                [self.contents addObject:@""];
            }
            NSString *time = [data objectForKey:@"sendDate"];
            if (time) {
                [self.contents addObject:time];
            }else{
                [self.contents addObject:@""];
            }
            NSString *messageType = [data objectForKey:@"messageType"];
            if (messageType) {
                [self.contents addObject:messageType];
            }else{
                [self.contents addObject:@""];
            }
            NSString *title = [data objectForKey:@"title"];
            if (title) {
                [self.contents addObject:title];
            }else{
                [self.contents addObject:@""];
            }
            NSString *content = [data objectForKey:@"content"];
            if (content) {
                [self.contents addObject:content];
            }else{
                [self.contents addObject:@""];
            }
            [self.tableView reloadData];
        }else{
            [self alertSome:model.msg];
        }
    } failBlock:^(NSInteger code) {
        [self endLoading];
        [self alertSome:kNetWorkFail];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.contents count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MailDetailCell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"MailDetailCell"];
        cell.textLabel.font = [UIFont systemFontOfSize:14];
        cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
        cell.detailTextLabel.textColor = [UIColor blackColor];
        cell.detailTextLabel.numberOfLines = 0;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *titles = @[@"发件人",@"收件时间",@"消息类型",@"标题",@"内容"];
    cell.textLabel.text = titles[indexPath.row];
    if ([self.contents count] > indexPath.row) {
        cell.detailTextLabel.text = self.contents[indexPath.row];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *content = [_contents objectAtIndex:indexPath.row];
    CGSize size = [content caculateSizeWithFont:[UIFont systemFontOfSize:14] MaxSize:CGSizeMake(ScreenWidth - 100, CGFLOAT_MAX)];
    return size.height + 16;
}

/*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];
    
    // Configure the cell...
    
    return cell;
}
*/

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Table view delegate

// In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here, for example:
    // Create the next view controller.
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:<#@"Nib name"#> bundle:nil];
    
    // Pass the selected object to the new view controller.
    
    // Push the view controller.
    [self.navigationController pushViewController:detailViewController animated:YES];
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
